/*
 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 * 1. The origin of this software must not be misrepresented; you must not
 * claim that you wrote the original software. If you use this software
 * in a product, an acknowledgment in the product documentation would be
 * appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 * misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */

package org.box2dflash.dynamics.joints {
	import org.box2dflash.dynamics.joints.Joint;
	import org.box2dflash.dynamics.Body;
	import org.box2dflash.common.math.Vec2;
	import org.box2dflash.common.math.*;
	import org.box2dflash.dynamics.*;
	import org.box2dflash.common.Internal;
	use namespace Internal;

	/// Pulley joint definition. This requires two ground anchors,
	/// two dynamic body anchor points, max lengths for each side,
	/// and a pulley ratio.
	public class PulleyJointDef extends JointDef {
		public function PulleyJointDef() {
			type = Joint.e_pulleyJoint;
			groundAnchor1 = new Vec2(-1.0, 1.0);
			groundAnchor2 = new Vec2(1.0, 1.0);
			localAnchor1 = new Vec2(-1.0, 0.0);
			localAnchor2 = new Vec2(1.0, 0.0);
			length1 = 0.0;
			maxLength1 = 0.0;
			length2 = 0.0;
			maxLength2 = 0.0;
			ratio = 1.0;
			collideConnected = true;
		}

		public function initialize(b1:Body, b2:Body,
				ga1:Vec2, ga2:Vec2,
				anchor1:Vec2, anchor2:Vec2,
				r:Number):void {
			body1 = b1;
			body2 = b2;
			groundAnchor1.v(ga1);
			groundAnchor2.v(ga2);
			localAnchor1 = body1.localPoint(anchor1);
			localAnchor2 = body2.localPoint(anchor2);
			//Vec2 d1 = anchor1 - ga1;
			var d1X:Number = anchor1.x - ga1.x;
			var d1Y:Number = anchor1.y - ga1.y;
			//length1 = d1.length;
			length1 = Math.sqrt(d1X * d1X + d1Y * d1Y);
		
			//Vec2 d2 = anchor2 - ga2;
			var d2X:Number = anchor2.x - ga2.x;
			var d2Y:Number = anchor2.y - ga2.y;
			//length2 = d2.length;
			length2 = Math.sqrt(d2X * d2X + d2Y * d2Y);
		
			ratio = r;
			//Settings.Assert(ratio > Number.MIN_VALUE);
			var C:Number = length1 + ratio * length2;
			maxLength1 = C - ratio * PulleyJoint._minPulleyLength;
			maxLength2 = (C - PulleyJoint._minPulleyLength) / ratio;
		}

		/// The first ground anchor in world coordinates. This point never moves.
		public var groundAnchor1:Vec2 = new Vec2();
		/// The second ground anchor in world coordinates. This point never moves.
		public var groundAnchor2:Vec2 = new Vec2();
		/// The local anchor point relative to body1's origin.
		public var localAnchor1:Vec2 = new Vec2();
		/// The local anchor point relative to body2's origin.
		public var localAnchor2:Vec2 = new Vec2();
		/// The a reference length for the segment attached to body1.
		public var length1:Number;
		/// The maximum length of the segment attached to body1.
		public var maxLength1:Number;
		/// The a reference length for the segment attached to body2.
		public var length2:Number;
		/// The maximum length of the segment attached to body2.
		public var maxLength2:Number;
		/// The pulley ratio, used to simulate a block-and-tackle.
		public var ratio:Number;
	}
}
